package com.wanghua.mall.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.wanghua.mall.product.vo.SpuSaveVo;
import org.springframework.web.bind.annotation.*;

import com.wanghua.mall.product.entity.SpuInfoEntity;
import com.wanghua.mall.product.service.SpuInfoService;
import com.wanghua.common.utils.PageUtils;
import com.wanghua.common.utils.R;

import javax.annotation.Resource;


/**
 * spu信息
 *
 * @author wanghua
 * @email 2367279431@qq.com
 * @date 2021-08-24 18:52:27
 */
@RestController
@RequestMapping("/product/spuinfo")
public class SpuInfoController {

    @Resource
    private SpuInfoService spuInfoService;


    /**
     * 根据skuId查询spu信息
     *
     * @param skuId
     * @return
     */
    @GetMapping("/getSpuInfoBySkuId/{skuId}")
    public R getSpuInfoBySkuId(@PathVariable("skuId") Long skuId) {
        SpuInfoEntity spuInfoEntity = spuInfoService.getSpuInfoBySkuId(skuId);
        return R.ok().put("data", spuInfoEntity);
    }


    //商品上架
    @PostMapping("/up/{spuId}")
    public R productUp(@PathVariable("spuId") Long spuId) {
        spuInfoService.productUp(spuId);
        return R.ok();
    }

    /**
     * 条件列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = spuInfoService.queryPageCondition(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SpuInfoEntity spuInfo = spuInfoService.getById(id);
        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存关于商品的很多数据
     */
    @PostMapping("/save")
    public R save(@RequestBody SpuSaveVo spuSaveVo) {
        spuInfoService.saveDetail(spuSaveVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody SpuInfoEntity spuInfo) {
        spuInfoService.updateById(spuInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        spuInfoService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
